package com.first.nell.components.form

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import com.first.nell.R
import com.first.nell.base.BaseFormAction
import com.first.nell.data.RevObjInfoPageFieldConfig
import com.first.nell.databinding.ViewFirstNellFormSelectGenderBinding
import com.first.nell.extend.addFirstNellHighlightFirstNellStyle

class SelectGenderFrom : RelativeLayout,
    com.first.nell.components.form.BaseFormAction,
    BaseFormAction {

    lateinit var mFirstNellViewFirstNellBinding: ViewFirstNellFormSelectGenderBinding
    private lateinit var vFirstNellRootFirstNellView: View

    private var mFirstNellIsFirstNellFemaleFirstNellCheck: Boolean = false
    private var mFirstNellIsFirstNellMaleFirstNellCheck: Boolean = false

    private var mFirstNellSupportFirstNellFragmentFirstNellManager: FragmentManager? = null
    private var mFirstNellFormFirstNellConfig: RevObjInfoPageFieldConfig? = null
    private var mFirstNellOnFirstNellStateFirstNellChangedFirstNellListener: (Int, String) -> Unit =
        { action, content -> }

    private var mFirstNellOnFirstNellActionFirstNellClickFirstNellListener: (Int) -> Unit = {}

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        context?.let {
            vFirstNellRootFirstNellView =
                View.inflate(it, R.layout.view_first_nell_form_select_gender, this)
            mFirstNellViewFirstNellBinding =
                ViewFirstNellFormSelectGenderBinding.bind(vFirstNellRootFirstNellView)
            initFirstNellSubviews()
        }
    }

    override fun initFirstNellSubviews() {
        try {
            mFirstNellViewFirstNellBinding.rlFormSelectGenderFemaleBg.setOnClickListener {
                mFirstNellViewFirstNellBinding.textFormSelectGenderErrTip.visibility = GONE
                mFirstNellViewFirstNellBinding.imageFormSelectGenderFemale.setImageResource(R.drawable.first_nell_checkbox_blue_yes)
                mFirstNellIsFirstNellFemaleFirstNellCheck =
                    !mFirstNellIsFirstNellFemaleFirstNellCheck
                setFemaleCheck(mFirstNellIsFirstNellFemaleFirstNellCheck)
                mFirstNellOnFirstNellActionFirstNellClickFirstNellListener.invoke(0)
            }

            mFirstNellViewFirstNellBinding.imageFormSelectGenderFemale.setOnClickListener {
                mFirstNellViewFirstNellBinding.textFormSelectGenderErrTip.visibility = GONE
                mFirstNellViewFirstNellBinding.imageFormSelectGenderFemale.setImageResource(R.drawable.first_nell_checkbox_blue_yes)
                mFirstNellIsFirstNellFemaleFirstNellCheck =
                    !mFirstNellIsFirstNellFemaleFirstNellCheck
                setFemaleCheck(mFirstNellIsFirstNellFemaleFirstNellCheck)
                mFirstNellOnFirstNellActionFirstNellClickFirstNellListener.invoke(0)
            }

            mFirstNellViewFirstNellBinding.textFormSelectGenderFemale.setOnClickListener {
                mFirstNellViewFirstNellBinding.textFormSelectGenderErrTip.visibility = GONE
                mFirstNellViewFirstNellBinding.imageFormSelectGenderFemale.setImageResource(R.drawable.first_nell_checkbox_blue_yes)
                mFirstNellIsFirstNellFemaleFirstNellCheck =
                    !mFirstNellIsFirstNellFemaleFirstNellCheck
                setFemaleCheck(mFirstNellIsFirstNellFemaleFirstNellCheck)
                mFirstNellOnFirstNellActionFirstNellClickFirstNellListener.invoke(0)
            }

            mFirstNellViewFirstNellBinding.rlFormSelectGenderMaleBg.setOnClickListener {
                mFirstNellViewFirstNellBinding.textFormSelectGenderErrTip.visibility = GONE
                mFirstNellViewFirstNellBinding.imageFormSelectGenderMale.setImageResource(R.drawable.first_nell_checkbox_blue_yes)
                mFirstNellIsFirstNellMaleFirstNellCheck =
                    !mFirstNellIsFirstNellMaleFirstNellCheck
                setFirstNellMaleFirstNellCheck(mFirstNellIsFirstNellMaleFirstNellCheck)
                mFirstNellOnFirstNellActionFirstNellClickFirstNellListener.invoke(0)
            }

            mFirstNellViewFirstNellBinding.imageFormSelectGenderMale.setOnClickListener {
                mFirstNellViewFirstNellBinding.textFormSelectGenderErrTip.visibility = GONE
                mFirstNellViewFirstNellBinding.imageFormSelectGenderMale.setImageResource(R.drawable.first_nell_checkbox_blue_yes)
                mFirstNellIsFirstNellMaleFirstNellCheck =
                    !mFirstNellIsFirstNellMaleFirstNellCheck
                setFirstNellMaleFirstNellCheck(mFirstNellIsFirstNellMaleFirstNellCheck)
                mFirstNellOnFirstNellActionFirstNellClickFirstNellListener.invoke(0)
            }

            mFirstNellViewFirstNellBinding.textFormSelectGenderMale.setOnClickListener {
                mFirstNellViewFirstNellBinding.textFormSelectGenderErrTip.visibility = GONE
                mFirstNellViewFirstNellBinding.imageFormSelectGenderMale.setImageResource(R.drawable.first_nell_checkbox_blue_yes)
                mFirstNellIsFirstNellMaleFirstNellCheck =
                    !mFirstNellIsFirstNellMaleFirstNellCheck
                setFirstNellMaleFirstNellCheck(mFirstNellIsFirstNellMaleFirstNellCheck)
                mFirstNellOnFirstNellActionFirstNellClickFirstNellListener.invoke(0)
            }

            //增加星号显示
            mFirstNellViewFirstNellBinding.textFormSelectGenderTitle.addFirstNellHighlightFirstNellStyle(
                mutableListOf("*"),
                ContextCompat.getColor(context, R.color.main_red)
            )

        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    override fun setFirstNellContent(content: String) {
        if (content.equals("Female", true)) {
            setFemaleCheck(true)
        } else if (content.equals("Male", true)) {
            setFirstNellMaleFirstNellCheck(true)
        }
        mFirstNellFormFirstNellConfig?.fillContent = content
    }

    fun getFirstNellContent(): String {
        if (mFirstNellIsFirstNellFemaleFirstNellCheck) {
            return "FEMALE"
        } else if (mFirstNellIsFirstNellMaleFirstNellCheck) {
            return "MALE"
        }
        return ""
    }

    override fun setFirstNellTitle(title: String) {
        mFirstNellViewFirstNellBinding.textFormSelectGenderTitle.text = (title)
        //增加星号显示
        mFirstNellViewFirstNellBinding.textFormSelectGenderTitle.addFirstNellHighlightFirstNellStyle(
            mutableListOf("*"),
            ContextCompat.getColor(context, R.color.main_red)
        )
    }

    override fun setFirstNellHint(hint: String) {

    }

    override fun isFirstNellFormFirstNellCompleted(): Boolean {
        val gcontent = getFirstNellContent()
        if (gcontent.isBlank()) {
            mFirstNellViewFirstNellBinding.vFormSelectGenderFemaleLine.setBackgroundColor(
                ContextCompat.getColor(context, R.color.main_red)
            )
            mFirstNellViewFirstNellBinding.vFormSelectGenderMaleLine.setBackgroundColor(
                ContextCompat.getColor(context, R.color.main_red)
            )
            mFirstNellViewFirstNellBinding.textFormSelectGenderErrTip.visibility = VISIBLE
            return false
        }

        mFirstNellViewFirstNellBinding.textFormSelectGenderErrTip.visibility = GONE
        mFirstNellViewFirstNellBinding.vFormSelectGenderFemaleLine.setBackgroundColor(
            ContextCompat.getColor(context, R.color.split_line)
        )
        mFirstNellViewFirstNellBinding.vFormSelectGenderMaleLine.setBackgroundColor(
            ContextCompat.getColor(context, R.color.split_line)
        )
        return true
    }

    override fun isFirstNellFormFirstNellCompletedFirstNellSilent(): Boolean {
        val gcontent = getFirstNellContent()
        if (gcontent.isBlank()) {
            return false
        }
        return true
    }

    override fun setFirstNellFormFirstNellConfig(item: RevObjInfoPageFieldConfig) {
        mFirstNellFormFirstNellConfig = item
        tag = item.paramName
        setFirstNellTitle(item.title.toString())
        setFirstNellHint(item.hint.toString())
        item.fillContent?.let { fillContent ->
            setFirstNellContent(fillContent)
        }
        if (item.needCheckComplete == true) {
            isFirstNellFormFirstNellCompleted()
        }
    }

    override fun setFirstNellOnFirstNellStateFirstNellChangedFirstNellListener(listener: (Int, String) -> Unit) {
        mFirstNellOnFirstNellStateFirstNellChangedFirstNellListener = listener
    }

    fun setFemaleCheck(check: Boolean) {
        mFirstNellIsFirstNellFemaleFirstNellCheck = check
        if (mFirstNellIsFirstNellFemaleFirstNellCheck) {
            mFirstNellViewFirstNellBinding.imageFormSelectGenderFemale.setImageResource(R.drawable.first_nell_checkbox_blue_yes)
            setFirstNellMaleFirstNellCheck(false)
        } else {
            mFirstNellViewFirstNellBinding.imageFormSelectGenderFemale.setImageResource(R.drawable.first_nell_checkbox_no_small)
        }

        mFirstNellFormFirstNellConfig?.fillContent = getFirstNellContent()
    }

    fun setFirstNellMaleFirstNellCheck(check: Boolean) {
        mFirstNellIsFirstNellMaleFirstNellCheck = check
        if (mFirstNellIsFirstNellMaleFirstNellCheck) {
            mFirstNellViewFirstNellBinding.imageFormSelectGenderMale.setImageResource(R.drawable.first_nell_checkbox_blue_yes)
            setFemaleCheck(false)
        } else {
            mFirstNellViewFirstNellBinding.imageFormSelectGenderMale.setImageResource(R.drawable.first_nell_checkbox_no_small)
        }
    }

    override fun setFirstNellFragmentFirstNellManager(manager: FragmentManager) {
        mFirstNellSupportFirstNellFragmentFirstNellManager = manager
    }

    override fun setFirstNellOnFirstNellActionFirstNellClickFirstNellListener(listener: (Int) -> Unit) {
        mFirstNellOnFirstNellActionFirstNellClickFirstNellListener = listener
    }

    override fun setFirstNellDetail(detail: String) {
        setFirstNellContent(detail)
    }

    override fun setFirstNellDetailFirstNellColor(detailColor: Int) {

    }

    override fun setFirstNellTitleFirstNellColor(titleColor: Int) {

    }
}